package lg.minecraft.plugin.pokemonmenu.gui.edit;

import com.cobblemon.mod.common.api.pokemon.feature.FlagSpeciesFeature;
import com.cobblemon.mod.common.api.pokemon.feature.SpeciesFeature;
import com.cobblemon.mod.common.pokemon.FormData;
import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.gui.EditInventor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class EditFormInventor extends LGInventory {

    private static final int[] GLASS_ARRAY = {45, 46, 47, 48, 50, 51, 52, 53};
    private static final int PAGE_SIZE = 45; // 每页显示的数量

    private final Pokemon pokemon;
    private final List<FormData> forms;

    public EditFormInventor(Pokemon pokemon) {
        super(6, LGPokemonMenu.getMessage().getGuiTitleEditForm());
        this.pokemon = pokemon;
        this.forms = pokemon.getSpecies().getForms();
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        int size = Math.min(forms.size(), PAGE_SIZE);
        for (int i = 0; i < size; i++) {
            FormData form = forms.get(i);
            ItemStack stack = form.equals(pokemon.getForm()) ? LGPokemonMenu.getGuiItem().getEditFormSelected(form.getName()) : LGPokemonMenu.getGuiItem().getEditFormUnSelected(form.getName());
            inventory.setItem(i, stack);
        }

        for (int slot : GLASS_ARRAY) inventory.setItem(slot, LGPokemonMenu.getGuiItem().getBorder());

        inventory.setItem(49, LGPokemonMenu.getGuiItem().getBack());
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();

        int size = Math.min(forms.size(), PAGE_SIZE);

        if (slot == 49) {
            new EditInventor(pokemon).openInventory(player);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot >= 0 && slot < size) {
            FormData formData = forms.get(slot);
            String formDataName = formData.getName().toLowerCase();
            pokemon.setForm(formData);
            for (SpeciesFeature feature : pokemon.getFeatures()) {
                String featureName = feature.getName();
                if (feature instanceof FlagSpeciesFeature flagSpeciesFeature) {
                    flagSpeciesFeature.setEnabled(featureName.contains(formDataName));
                }
            }
            pokemon.updateAspects();
            new EditInventor(pokemon).openInventory(player);
            player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0F, 1.8F);
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
    }

}

